<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;
use app\admin\model\UserModel;
use \app\admin\controller\Permissions;
use app\admin\model\WithdrawalsModel;
use \think\Db;
use excel\ExportService;
class Withdrawals extends Permissions
{
    public function index()
    {	
    	$param = input('param.');
    	$where = [];
    	$and_where = [];
    	if (isset($param['nickname']) && !empty($param['nickname'])) {
            $where['name'] = ['like', '%' . $param['nickname'] . '%'];
        }
        if (isset($param['phone']) && !empty($param['phone'])) {
            $where['phone'] = ['like', '%' . $param['phone'] . '%'];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $where['status'] = $param['status'];
        }

        if (isset($param['start']) && !empty($param['start'])) {
            // $param['start'] = strtotime($param['start']);
            $where['create_time'] = ['>=', $param['start']];
        }

        if (isset($param['end']) && !empty($param['end'])) {
            // $param['end'] = strtotime($param['end'] . ' 23:59:59');
            $and_where['create_time'] = ['<=', $param['end']];
        }

    	//实例化提现类
    	$model=new WithdrawalsModel();
    	$data = $model->where($where)->where($and_where)->order('id desc')->paginate(20,false,['query'=>$this->request->param()]);
    	foreach ($data as $key => $value) {
    		$data[$key]['types']=WithdrawalsModel::TYPES[$value['types']];
            $data[$key]['operat'] = "<a href='javascript:operat(" . $value['id'] . ")'>查看详情</a>";
    		$data[$key]['operate']=$this->mButton($value['id'],$value['status']);
    		$data[$key]['status']=WithdrawalsModel::STATUS[$value['status']];
            $data[$key]['created_at']=$value['create_time'];
    	}
    	$this->assign('param',json_encode($param)??"");
        $this->assign('data',$data);
        return $this->fetch();
    }
    //通过
    public function edit()
    {
       $id=input('id');
       $res=WithdrawalsModel::where(['id'=>$id])->setField('status',2);
       if($res){
        return json(['status'=>200,'msg'=>'操作成功']);
       }else{
        return json(['status'=>300,'msg'=>'操作失败']);
       }

    }

    //驳回
    public function del()
    {
       $id=input('id');
       $res=WithdrawalsModel::where(['id'=>$id])->setField('status',3);
       if($res){
        return json(['status'=>200,'msg'=>'操作成功']);
       }else{
        return json(['status'=>300,'msg'=>'操作失败']);
       }

    }
    public function toexcel()
    {
        $param = input('param.');
        $where = [];
        $and_where = [];
        if (isset($param['nickname']) && !empty($param['nickname'])) {
            $where['name'] = ['like', '%' . $param['nickname'] . '%'];
        }
        if (isset($param['phone']) && !empty($param['phone'])) {
            $where['phone'] = ['like', '%' . $param['phone'] . '%'];
        }

        if (isset($param['start']) && !empty($param['start'])) {
            $param['start'] = strtotime($param['start']);
            $where['create_time'] = ['>=', $param['start']];
        }
        if (isset($param['status']) && !empty($param['status'])) {
            $where['status'] = $param['status'];
        }

        if (isset($param['end']) && !empty($param['end'])) {
            $param['end'] = strtotime($param['end'] . ' 23:59:59');
            $and_where['create_time'] = ['<=', $param['end']];
        }

        //实例化提现类
        $model=new WithdrawalsModel();
        $data = $model->where($where)->where($and_where)->select();
        $data = json_decode(json_encode($data),true);
        foreach ($data as $key => $value) {
            unset($data[$key]['uid']);
            unset($data[$key]['code']);
            unset($data[$key]['code']);
            $data[$key]['types']=WithdrawalsModel::TYPES[$value['types']];
            $data[$key]['status']=WithdrawalsModel::STATUS[$value['status']];
        }
        $excel = new ExportService();
        
        // $first = ['A1'=>'编号','B1'=>'提现人姓名','C1'=>'账户余额','D1'=>'手机号','E1'=>'金额','F1'=>'手续费','G1'=>'实际到账金额','H1'=>'账户','I1'=>'状态','J1'=>'交易单号','J1'=>'提现类型','J1'=>'提现银行卡id'];
        $first = ['编号','提现人姓名','账户余额','手机号','金额','手续费','实际到账金额','账户','状态','交易单号','提现类型','提现银行卡id','提交时间','操作时间'];
        
        if($data){
            $result = $excel->index($data,$first,"提现.xls","sheet1");
        }
        
        
    }
     //详情
    public function operat()
    {
        $id = input('param.id');
        $list = WithdrawalsModel::where(['id' => $id])->value('types');
        $uid = WithdrawalsModel::where(['id' => $id])->value('uid');
        $bank_id = WithdrawalsModel::where(['id' => $id])->value('bank_id');
        if ($list == 1) {
            $orderDetail = db('bind')->where(['users_id' => $uid])->find();
        } else {
            $orderDetail = db('bind')->where(['users_id' => $uid, 'id' => $bank_id])->find();
        }
        return json($orderDetail);

    }
    //按钮
    private function mButton($id, $type = '')
    {
        if ($type == 1) {
            return "<a href='javascript:edit($id)'><button type='button' class='layui-btn layui-btn-danger layui-btn-sm'><i class=''></i> 通 过</button></a>&nbsp<a href='javascript:del($id)'><button type='button' class='layui-btn layui-btn-warm layui-btn-sm'><i class=''></i> 驳 回</button></a>";
        }
        if ($type == 2) {
            return '<a href="javascript:"><button type="button" class="layui-btn layui-btn-danger layui-btn-sm"><i class=""></i>已通过</button></a>';
        }
        if ($type == 3) {
          return '<a href="javascript:"><button type="button" class="layui-btn layui-btn-warm layui-btn-sm"><i class=""></i> 已驳回</button></a>';
        }
    }
}
